package Commandes;

import java.util.*;

public class InvokerCommandeFichier extends Observable{
    
    private static InvokerCommandeFichier invoker;
    private Stack pileAnnuler;
    private Stack pileRefaire;
      
    
    private InvokerCommandeFichier() {
        
        this.pileAnnuler=new Stack();
        this.pileRefaire=new Stack();

    }
    
    
    public static InvokerCommandeFichier getInvoker(){
        
        if(invoker==null){
            
            invoker=new InvokerCommandeFichier();
        }
        
        return invoker;
    }
    
    public void launchCommande(Commande c){
        
        if(c.execute()){
        this.pileAnnuler.push(c);
        setChanged();
        notifyObservers();}
    }
    
    public void cancelCommande(){
        
        if(!this.pileAnnuler.empty()){
            Commande c=(Commande)this.pileAnnuler.pop();
            if(c.cancel()){
            pileRefaire.push(c);
            this.setChanged();
            this.notifyObservers();
            this.clearChanged();}
        }
    }
    
    public void refaireCommande(){
        if(!this.pileRefaire.empty()){
            Commande c=(Commande)this.pileRefaire.pop();
            if(c.execute()){
            pileAnnuler.push(c);
            this.setChanged();
            this.notifyObservers();
            this.clearChanged();}
        }
    }
    
    
    public Commande getCommandePileAnnuler(){
        if (!(pileAnnuler.empty())){
            return (Commande)pileAnnuler.peek();
        }else{
            return null;
        }
    }
    
    public Commande getCommandePileRefaire(){
        
        if (!pileRefaire.empty()){
            return (Commande)pileRefaire.peek();
        }else{
            return null;
        }
    }
    
    
}
